// pages/task-plan/index.js

var util = require('../../utils/util.js');
var api = require('../../config/api.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
			dateTime: '',//年月
			collapseValue: [0,1,2],
			targets:[],//本月目标
			tasks:[],//本月任务统计
			qualitys:[],//PC端任务指标
			dateOptions:[],//日期星期数组
			selectDate: {},//选中的日期星期
			planTaskList: [],//计划任务list
			finishTaskList: [],//完成任务list
			tabActive: 0,//日期星期选择数组下标值
			show: false,
			overlayShow: false,//遮罩层显示
			options: [
				{ name: '客户跟进', icon: 'link' },
				{ name: '政府维护', icon: 'link' },
				{ name: '日常维护', icon: 'link' },
				{ name: '其他工作', icon: 'link' }
			]
		},
		/** 收起展开 */
    onChange(event) {
			this.setData({
				collapseValue: event.detail,
			});
		},
		/** 任务明细 */
		toTaskDetail (e) {
			const { id } = e.currentTarget.dataset;
			wx.navigateTo({
				url: '../task-detail/index?id=' + id,
			});
		},
		/** 新建任务 */
		replanBtn(){
			this.setData({ show: true });
		},
		/** 日期星期选择事件 */
		selectTabs (e) {
			const { value } = e.currentTarget.dataset;
			this.setData({ tabActive: value });
			this.getCrmIndexTaskListData();
		},
		/** 新建任务计划选择事件 */
		onSelect (e) {
			const index = e.detail.index;
			const linkUrl = [
				'../setup-customer/index',
				'../setup-gov/index',
				'../setup-normal/index',
				'../setup-other/index'
			];
			this.setData({ show: false });
			wx.navigateTo({
				url: linkUrl[index],
			});
		},
		onCancel () {
			this.setData({ show: false });
		},
		/**本月任务统计跳转事件 */
		toTaskList (e) {
			const { param } = e.currentTarget.dataset;
			wx.navigateTo({
				url: `${param.url}?name=${param.name}`,
			});
		},
		/** 年月日期 */
		getDateTime(val) {
			this.setData({
				dateTime: val.detail.value
			});
			this.getCrmIndexData();
			this.getCrmIndexDateOptions();
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
			this.setData({
				overlayShow: true
			});
			var d = new Date();
			var m = d.getMonth() + 1;
			m = m < 10 ? "0" + m : m;
			var dateStr = d.getFullYear() + "-" + m;
			console.log(dateStr);
			this.setData({
				dateTime: dateStr
			});
			this.getCrmIndexData();
			this.getCrmIndexDateOptions();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

		},

		/** 获取任务计划首页数据 */
		getCrmIndexData(){
			var that = this;
			util.request(api.getCrmIndexData, {
				id: wx.getStorageSync('appUserInfo').id,
				userId: wx.getStorageSync('appUserInfo').userId,
				deptId: wx.getStorageSync('appUserInfo').deptId,
				companyId: wx.getStorageSync('appUserInfo').companyId,
				dateTime: that.data.dateTime
			}, 'GET').then(res => {
				if (res.result == 1) {
					that.setData({
						targets: res.info.targets,
						tasks: res.info.tasks,
						qualitys: res.info.qualitys,
						overlayShow: false
					});
				}
			});
		},

		/** 获取CRM首页日期星期数据 */
		getCrmIndexDateOptions(){
			var that = this;
			util.request(api.getCrmIndexDateOptions, {
				dateTime: that.data.dateTime
			}, 'GET').then(res => {
				if (res.result == 1) {
					that.setData({
						dateOptions: res.info.list,
						tabActive: res.info.index,
						selectDate: {
							date: res.info.list[res.info.index].dateStr,
							week: res.info.list[res.info.index].week
						}
					});
					that.getCrmIndexTaskListData();
				}
			});
		},

		/** 获取CRM首页时间日期任务数据 */
		getCrmIndexTaskListData(){
			var that = this;
			util.request(api.getCrmIndexTaskListData, {
				userId: wx.getStorageSync('appUserInfo').id,
				dateStr: that.data.dateOptions[that.data.tabActive].dateStr,
				date: that.data.dateOptions[that.data.tabActive].date,
				week: that.data.dateOptions[that.data.tabActive].week
			}, 'GET').then(res => {
				if (res.result == 1) {
					that.setData({
						planTaskList: res.info.planTasks,
						finishTaskList: res.info.completeTasks,
					});
				}
			});
		},
})